package com.xw.tools.util;

import cn.hutool.core.util.XmlUtil;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;

import java.util.Map;

/**
 * Created by ww on 2019/3/26.
 */
public class GroovyUtil {

    public static Object run(String script, Map<String, Object> contextMap) {
        Binding binding = new Binding();
        if (contextMap != null) {
            for (Map.Entry<String, Object> entry : contextMap.entrySet()) {
                binding.setVariable(entry.getKey(), entry.getValue());
            }
        }

        GroovyShell shell = new GroovyShell(binding);
        Object value = shell.evaluate(script);
        return value;
    }

    public static void main(String[] args) {
        String str = "println 100";
//        run(str, null);
        String format = XmlUtil.format("<xml><student><age>111</age><name>zs</name></student></xml>");
        System.out.println(format);
    }
}
